package com.example.aircraft;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.example.aircraft.aircraft.HeroAircraft;
import com.example.aircraft.database.MySqlManager;
import com.example.aircraft.database.User_Data;
import com.example.aircraft.gamemode.Game;
import com.example.aircraft.gamemode.GameBase;
import com.example.aircraft.panel.DualSignPanel;
import com.example.aircraft.panel.SignPanel;
import com.example.aircraft.prop.PropFactory;

public class ToolController {
    private GameBase game;
    private User_Data user_data;

    public ToolController(GameBase game) {
        this.game = game;
        this.game.setOnTouchListener(new View.OnTouchListener() {
            private static final String TAG = "success";

            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    float X = event.getX();
                    float Y = event.getY();
                    if (((0 < X) && (X < 100)) && ((1440 < Y) && (Y < 1630))) {
                        toolConsume(0);
                        Log.d(TAG, "onTouch: Blood");
                        return true;
                    } else if (((0 < X) && (X < 100)) && ((1630 < Y) && (Y < 1820))) {
                        toolConsume(1);
                        Log.d(TAG, "onTouch: Bullet");
                        return true;
                    } else if (((0 < X) && (X < 100)) && ((1820 < Y) && (Y < 2000))) {
                        toolConsume(2);
                        Log.d(TAG, "onTouch: Bomb");
                        return true;
                    }
                    return false;
                }
                return false;
            }

        });

    }

    protected void toolConsume(int i){
        user_data = DualSignPanel.getUser_data();
        boolean success = false;
        switch (i){
            case 0:
                if(user_data.getSHEILD()>0) {
                    user_data.updateSHEILD(0);
                    success = true;
                    //准备偷懒，用道具工厂把道具生成在英雄机位置
                    PropFactory.setProp("Blood", HeroAircraft.newInstance().getLocationX(),
                            HeroAircraft.newInstance().getLocationY());
                }
                break;
            case 1:
                if(user_data.getCLUSTER()>0) {
                    user_data.updateCLUSTER(0);
                    success = true;
                    //activeTool(1);
                    PropFactory.setProp("Bullet",HeroAircraft.newInstance().getLocationX(),
                            HeroAircraft.newInstance().getLocationY());
                }
                break;
            case 2:
                if(user_data.getHEMOPHAGIA()>0) {
                    user_data.updateHEMOPHAGIA(0);
                    success = true;
                    //activeTool(2);
                    PropFactory.setProp("Bomb",HeroAircraft.newInstance().getLocationX(),
                            HeroAircraft.newInstance().getLocationY());
                }
                break;
            default:
                break;
        }
        if(success) {
            user_data = DualSignPanel.getUser_data();
        }
    }
}
